using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace server.Models;

public class BorrowRecord{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id {get;set;}
    public User User { get; set; }=null!;
    public Book Book { get; set; }=null!;
    public DateTime BorrowDate{get;set;}=DateTime.Now;
    public DateTime? ReturnDate{get;set;}

    [NotMapped]
    public bool IsReturned=> !(ReturnDate is null);

    public void Return(){
        ReturnDate=DateTime.Now;
        if (ReturnDate < BorrowDate)
        {
            throw new Exception($"Return Date[{ReturnDate}] is earlier than Borrow Date[{BorrowDate}]!");
        }
    }

    public BorrowRecord()
    {
        
    }

    public BorrowRecord(User u, Book b){
        User=u;
        Book=b;
    }
}